package com.ziytek.web.citizen.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ziytek.web.citizen.model.rsp.QueryFileNameRsp;
import com.ziytek.web.citizen.service.itf.ExportFileNameService;
import com.ziytek.web.citizen.util.LocationUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @className ExportFileNameServiceImpl
 * @description: 可导出文件名称实现类
 * @jdkVersion: 1.8
 * @author: fhx
 * @date: 2024/7/2 9:53
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ExportFileNameServiceImpl implements ExportFileNameService {
    @Override
    public QueryFileNameRsp queryFileName() {
        QueryFileNameRsp rsp = new QueryFileNameRsp();
        rsp.error();
        String locationAddress = LocationUtils.getLocation();
        log.info("当前项目数据库文件路径：{}", locationAddress);
        // 文件不存在
        File folder = new File(locationAddress + "/download");
        if (!folder.exists()) {
            log.info("文件夹[{}]不存在", locationAddress + "/download");
            rsp.setRetMsg(String.format("文件夹[%s]不存在", locationAddress + "/download"));
            return rsp;
        }

        List<String> fileNames = new ArrayList<>();
        if (folder.isDirectory()) {
            File[] files = folder.listFiles();
            if (files == null || files.length == 0) {
                return rsp;
            }

            for (File file : files) {
                if (file.isDirectory()) {
                    continue;
                }
                fileNames.add(file.getName());
            }
        }

        if (!CollectionUtils.isEmpty(fileNames)) {
            rsp.setData(fileNames);
        }

        rsp.success();
        return rsp;
    }

    @Override
    public QueryFileNameRsp queryFileNameByCabinetId(String cabinetId) {
        QueryFileNameRsp rsp = new QueryFileNameRsp();
        rsp.error();
        String locationAddress = LocationUtils.getLocation();
        log.info("当前项目数据库文件路径：{}", locationAddress);
        // 文件不存在
        File folder = new File(locationAddress + "/download/" + cabinetId);
        if (!folder.exists()) {
            log.info("文件夹[{}]不存在", locationAddress + "/download/" + cabinetId);
            rsp.setRetMsg(String.format("文件夹[%s]不存在", locationAddress + "/download/" + cabinetId));
            return rsp;
        }

        List<String> fileNames = new ArrayList<>();
        if (folder.isDirectory()) {
            File[] files = folder.listFiles();
            if (files == null || files.length == 0) {
                return rsp;
            }

            for (File file : files) {
                if (file.isDirectory()) {
                    continue;
                }
                fileNames.add(file.getName());
            }
        }

        if (!CollectionUtils.isEmpty(fileNames)) {
            rsp.setData(fileNames);
        }

        rsp.success();
        return rsp;
    }
}
